package models

import (
	"gorm.io/gorm"
	"order_srv/basic/config"
	"time"
)

// 用户分享优惠券表
type UserShareCoupons struct {
	Id         uint64         `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;" json:"id"`
	SharerId   int64          `gorm:"column:sharer_id;type:bigint;comment:用户id;not null;" json:"sharer_id"`                          // 用户id
	CouponId   int64          `gorm:"column:coupon_id;type:bigint;comment:被分享的优惠券id;not null;" json:"coupon_id"`                     // 被分享的优惠券id
	ShareToke  string         `gorm:"column:share_toke;type:varchar(255);comment:分享唯一凭证链接;not null;" json:"share_toke"`              // 分享唯一凭证链接
	ReceiverId int64          `gorm:"column:receiver_id;type:bigint;comment:被分享者用户id;default:NULL;" json:"receiver_id"`              // 被分享者用户id
	ExpireTime time.Time      `gorm:"column:expire_time;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"expire_time"` // 优惠券过期时间
	CreatedAt  time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt  time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt  gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (usc *UserShareCoupons) TableName() string {
	return "user_share_coupons"
}

// 用户分享优惠券表添加
func (usc *UserShareCoupons) UserShareCouponsAdd() error {
	return config.DB.Create(&usc).Error
}

// 用户分享优惠券表查询
func (usc *UserShareCoupons) GetUserShareCoupons() error {
	return config.DB.Where("sharer_id = ? and coupon_id = ?", usc.SharerId, usc.CouponId).First(&usc).Error
}
